package cn.edu.ccut.refactor;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * 
 * @author Bai Yucheng 2008-4-16
 * 
 */
public class RenameSaturnModuleWizard extends RefactoringWizard {
	Refactoring fRefactoring;
	
	public RenameSaturnModuleWizard(Refactoring refactoring, int i) {
		super(refactoring, i);
		setDefaultPageTitle(Messages.getString("RenameSaturnModuleWizard.0")); //$NON-NLS-1$
		fRefactoring = refactoring;
	}

	protected void addUserInputPages() {
		org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor refactoringprocessor = ((RenameRefactoring) getRefactoring())
				.getProcessor();
		String s = ((AbstractRenameProcessor) refactoringprocessor)
				.getCurrentElementName();
		RenameInputWizardPage renameinputwizardpage = createInputPage("", s); //$NON-NLS-1$
		addPage(renameinputwizardpage);
	}

	protected RenameInputWizardPage createInputPage(String s, String s1) {
		return new RenameInputWizardPage(s, true, s1) {

			protected RefactoringStatus validateTextField(String s2) {
				return validateNewName(s2);
			}

			public void createControl(Composite composite) {
				Composite composite1 = new Composite(composite, 0);
				setControl(composite1);
				initializeDialogUnits(composite1);
				composite1.setLayout(new GridLayout());
				Composite composite2 = new Composite(composite1, 0);
				composite2.setLayoutData(new GridData(768));
				GridLayout gridlayout = new GridLayout();
				gridlayout.numColumns = 2;
				composite2.setLayout(gridlayout);
				Label label = new Label(composite2, 0);
				label.setText(Messages.getString("RenameSaturnModuleWizard.2")); //$NON-NLS-1$
				Text text = createTextInputField(composite2);
				RenameRefactoring renamerefactoring = (RenameRefactoring) getRefactoring();
				renamerefactoring.getProcessor();
				text.setLayoutData(new GridData(768));
				text.selectAll();
				Dialog.applyDialogFont(composite1);
			}

		};
	}

	protected RefactoringStatus validateNewName(String s)
    {
		RefactoringStatus refactoringStatus = null;
        RefactoringProcessor refactoringprocessor;
        refactoringprocessor = ((RenameRefactoring)getRefactoring()).getProcessor();
        ((AbstractRenameProcessor)refactoringprocessor).setNewElementName(s);
        try {
			refactoringStatus = ((AbstractRenameProcessor)refactoringprocessor).checkNewElementName(s);
		} catch (CoreException e) {
			e.printStackTrace();
		}
		return refactoringStatus;
    }

}
